package com.shyb.lingxian.mine.presenter;

import android.content.Context;
import android.util.Log;

import com.shyb.lingxian.Util.AppUtil;
import com.shyb.lingxian.Util.CheckSumBuilder;
import com.shyb.lingxian.Util.SPUtils;
import com.shyb.lingxian.Util.StringUtils;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BasePresenter;
import com.shyb.lingxian.mine.bean.AddAddressSuccessBean;
import com.shyb.lingxian.mine.view.AddAdressView;
import com.shyb.lingxian.mine.view.AddressMangerView;
import com.shyb.lingxian.netWork.Const;
import com.shyb.lingxian.netWork.RequestCallback;

/**
 * Created by gs on 2018/3/26.
 */

public class AddAdressPresenter extends BasePresenter<AddAdressView>{

    public AddAdressPresenter(AddAdressView view) {
        super.attachView(view);
    }

    public void SaveNewAddress(final Context context){

        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String checkSum = CheckSumBuilder.getCheckSum("1", Const.NONCE, timestamp);
        String tickets = SPUtils.getString(context, "TICKETS", "");
        if (tickets.length()==0){
            Log.e("保存地址：","Tickets长度为0");
            return;
        }

        String realName = view.getRealName();
        if (realName == null || realName.length()==0){
            ToastUtil.showToast(context,"姓名不为空！");
            Log.i("realName:",realName);
            return;
        }
//        else if (!StringUtils.matherName(realName)){
//            ToastUtil.showToast(context,"姓名不符合规则！");
//            return ;
//        }

        String realPhone = view.getRealPhone();
        if (realPhone == null || realPhone.length() == 0){
            ToastUtil.showToast(context, "电话号码不为空！");
            return;
        }else if (!AppUtil.isPhoneNumber(realPhone)){
            ToastUtil.showToast(context,"电话号码不符合要求，请重写输入！");
            return;
        }

        String realProvice = view.getRealProvice();
        if (realProvice == null || realProvice.length()==0){
            ToastUtil.showToast(context,"收货地址不为空！");
            return;
        }

        String roomNumber = view.getRealAddress();
        if (roomNumber == null || roomNumber.length() == 0){
            ToastUtil.showToast(context,"请填写房间号");
            return;
        }

        String category = view.getCategory();
        if (category == null || category.length()==0){
            ToastUtil.showToast(context,"地址类型不为空！");
            return;
        }

        onSubscribe(services.SaveNewAddress(Const.secret, Const.NONCE, timestamp, checkSum, tickets, realName, realPhone, realProvice, roomNumber, category), new RequestCallback<AddAddressSuccessBean>() {
            @Override
            public void onSuccess(AddAddressSuccessBean model) {

            }

            @Override
            public void onFailure(String message) {

            }

            @Override
            public void onFinish() {

            }
        });
    }
}
